/**
 * Created by isoft-work 5 on 12/20/2014.
 */
$(document).ready(function(){
   if(checkLogin()){
      _userInfoPayment = null;
      _userShippayment = null;
      loadDataPaymentPage();
      bindEventPaymentPage();
   }
   else {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
});

function bindEventPaymentPage(){
   ISS.EVENT.bindEvent('#SameAddress', 'click', function () {
      ISS.EFFECTS.slideUp('#checkout-shipping-content', 'slow');
      _userShippayment = null;
   });

   ISS.EVENT.bindEvent('#DifferentAddress', 'click', function () {
      ISS.EFFECTS.slideDown('#checkout-shipping-content', 'slow');
   });

   ISS.EVENT.bindEvent('#checkoutBtnTop', 'click', function () {
      validatePaymentPage();
   });

   ISS.EVENT.bindEvent('#checkoutBtn', 'click', function () {
      validatePaymentPage();
   });

   ISS.EVENT.bindEvent('#payment_form_phone', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#ShippingAddressForm_phone', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });
}

function validatePaymentPage() {
   var _addressSend = ISS.DOM.getAtt('#SameAddress', 'checked');
   clearValidatePaymentPay();
   if(_addressSend) {
      if(validateUserInfoPaymentPage()) {
         sendInfoPaymentPage();
      }
      else {
         var htmlText = '<div id="message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Chưa nhập đầy đủ thông tin</div>\
                            </div>';
         ISS.DOM.before('#main', htmlText);
     }
   }
   else {
      var _validate1 = validateUserInfoPaymentPage();
      var _validate2 = validateUserSendPaymentPage();
      if(_validate2 && _validate1) {
         sendInfoPaymentPage();
      }
      else{
         htmlText = '<div id="message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Chưa nhập đầy đủ thông tin</div>\
                            </div>';
         ISS.DOM.before('#main', htmlText);
      }
   }
}

function sendInfoPaymentPage() {
   var _order = ISS.DOM.getAtt('#payment_form_order', 'checked');
   var paymentInfo = {};
   paymentInfo.thongTinThanhVien = _userInfoPayment;
   paymentInfo.note = ISS.DOM.getVal('#SalesOrderForm_note');
   if(!_userShippayment) {
      paymentInfo.nguoiNhan = null;
   }
   else {
      paymentInfo.nguoiNhan = _userShippayment;
   }

   if(!_order) {
      paymentInfo.type = 1;
      ISS.BACKEND.request({
         url: ISS.SESSION.getSession('contextPath') + '/service/payment/payPal/beforeCheckOut',
         headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
         },
         type: 'POST',
         dataType: 'json',
         data: JSON.stringify(paymentInfo),
         success: function (data) {
            var _typePaymentBaoKim = ISS.DOM.getAtt('#payment_form_baokim', 'checked');
            var _typePaymentNganLuong = ISS.DOM.getAtt('#payment_form_nganluong', 'checked');
            var _typePaymentPayPal = ISS.DOM.getAtt('#payment_form_paypal', 'checked');

            if(_typePaymentBaoKim) {
               ISS.DOM.setAtt('#payment_form', 'action',ISS.SESSION.getSession('contextPath') + '/service/payment/baoKim/payment');
            }
            if(_typePaymentPayPal) {
               ISS.DOM.setAtt('#payment_form', 'action',ISS.SESSION.getSession('contextPath') + '/service/payment/payPal/checkOut');
            }
            if(_typePaymentNganLuong) {
               ISS.DOM.setAtt('#payment_form', 'action',ISS.SESSION.getSession('contextPath') + '/service/payment/nganLuong/checkOut');
            }
            ISS.DOM.setAtt('#payment_form', 'autocapitalize', 'on');
            ISS.DOM.setAtt('#payment_form', 'autocorrect', 'on');
            $("#payment_form").submit();
         }
      });
   }
   else {
      paymentInfo.type = 0;
      ISS.BACKEND.request({
         url: ISS.SESSION.getSession('contextPath') + '/service/payment/insert',
         headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
         },
         type: 'POST',
         dataType: 'json',
         data: JSON.stringify(paymentInfo),
         success: function (data) {
            var responseInfo = data.responseObject;
            if(!responseInfo) {
               ISS.WINDOW.location('/jsp/user/user-check-bill.jsp');
            }
         }
      });
   }
}

function clearValidatePaymentPay() {
   ISS.DOM.removeClass('#payment_form_first_name', 'error');
   ISS.DOM.removeClass('#payment_form_last_name', 'error');
   ISS.DOM.removeClass('#payment_form_phone', 'error');
   ISS.DOM.removeClass('#payment_form_address', 'error');
   ISS.DOM.removeClass('#payment_form_gender', 'error');
   ISS.DOM.removeClass('#payment_form_day', 'error');
   ISS.DOM.removeClass('#payment_form_month', 'error');
   ISS.DOM.removeClass('#payment_form_year', 'error');

   ISS.DOM.removeClass('#ShippingAddressForm_first_name', 'error');
   ISS.DOM.removeClass('#ShippingAddressForm_last_name', 'error');
   ISS.DOM.removeClass('#ShippingAddressForm_phone', 'error');
   ISS.DOM.removeClass('#ShippingAddressForm_address1', 'error');

   ISS.DOM.remove('#message-error');
}

function validateUserInfoPaymentPage(){
  var _userInfo = {}, result;
   mapViewToUserInfoPaymentPage(_userInfo);
   if(!_userInfo.ten) {
      result = false;
      ISS.DOM.addClass('#payment_form_first_name', 'error');
   }
   if(!_userInfo.ho) {
      result = false;
      ISS.DOM.addClass('#payment_form_last_name', 'error');
   }
   if(!_userInfo.soDT) {
      result = false;
      ISS.DOM.addClass('#payment_form_phone', 'error');
   }
   if(!_userInfo.diaChi) {
      result = false;
      ISS.DOM.addClass('#payment_form_address', 'error');
   }
   if(!_userInfo.gioiTinh) {
      result = false;
      ISS.DOM.addClass('#payment_form_gender', 'error');
   }
   if(!_userInfo.day) {
      result = false;
      ISS.DOM.addClass('#payment_form_day', 'error');
   }
   if(!_userInfo.month) {
      result = false;
      ISS.DOM.addClass('#payment_form_month', 'error');
   }
   if(!_userInfo.year) {
      result = false;
      ISS.DOM.addClass('#payment_form_year', 'error');
   }
   if(_userInfo.ten && _userInfo.ho && _userInfo.soDT && _userInfo.diaChi && _userInfo.gioiTinh && _userInfo.day && _userInfo.month && _userInfo.year) {
      var _dateString = _userInfo.year + '/' + _userInfo.month + '/' + _userInfo.day + '/';
      _userInfo.ngaySinh = new Date(_dateString);
      delete _userInfo.year;
      delete _userInfo.month;
      delete _userInfo.day;
      _userInfoPayment = _userInfo;
      result = true;
   }
   return result;
}

function mapViewToUserInfoPaymentPage(userInfo) {
   userInfo.maTV = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   userInfo.ten = ISS.DOM.getVal('#payment_form_first_name');
   userInfo.ho = ISS.DOM.getVal('#payment_form_last_name');
   userInfo.soDT = ISS.DOM.getVal('#payment_form_phone');
   userInfo.diaChi = ISS.DOM.getVal('#payment_form_address');
   userInfo.gioiTinh = ISS.DOM.getVal('#payment_form_gender').toUpperCase();
   userInfo.day = ISS.DOM.getVal('#payment_form_day');
   userInfo.month = ISS.DOM.getVal('#payment_form_month');
   userInfo.year = ISS.DOM.getVal('#payment_form_year');
}

function validateUserSendPaymentPage(){
   var _userSend = {}, result;
   mapViewToUserSendPaymentPage(_userSend);
   if(!_userSend.ten) {
      result = false;
      ISS.DOM.addClass('#ShippingAddressForm_first_name', 'error');
   }
   if(!_userSend.ho) {
      result = false;
      ISS.DOM.addClass('#ShippingAddressForm_last_name', 'error');
   }
   if(!_userSend.soDT) {

      result = false;
      ISS.DOM.addClass('#ShippingAddressForm_phone', 'error');
   }
   if(!_userSend.diaChi) {
      result = false;
      ISS.DOM.addClass('#ShippingAddressForm_address1', 'error');
   }
   if(_userSend.diaChi && _userSend.soDT && _userSend.ho && _userSend.ten) {
      _userShippayment = _userSend;
      result = true;
   }
   return result;
}

function mapViewToUserSendPaymentPage(userSend) {
   userSend.ten = ISS.DOM.getVal('#ShippingAddressForm_first_name');
   userSend.ho = ISS.DOM.getVal('#ShippingAddressForm_last_name');
   userSend.soDT = ISS.DOM.getVal('#ShippingAddressForm_phone');
   userSend.diaChi = ISS.DOM.getVal('#ShippingAddressForm_address1');
}

function checkLogin() {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   if(useName) {
      return true;
   }
   else {
      return false;
   }
}

function loadDataPaymentPage() {
   getCartPaymentPage();
   getInfoUserPaymentPage();
}

function getCartPaymentPage(){
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getListCart',
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getCartPaymentPageComplete(responseInfo);
      }
   });
}

function getCartPaymentPageComplete(carts) {
   changePriceToString(carts);
   mapCartToPaymentPage(carts);
}

function mapCartToPaymentPage(carts) {
   carts.contextPath = ISS.SESSION.getSession('contextPath');
   var _cartHTML = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.Payment.listCart ,carts);
   delete carts.contextPath;
   ISS.DOM.html('#checkoutGrandTotal', _cartHTML);
}

function getInfoUserPaymentPage(){
   var user = {};
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   user.maTV = useName;
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/getInfoUser/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseInfo = data.responseObject;
         getInfoUserPaymentPageComplete(responseInfo);
      }
   });
}

function getInfoUserPaymentPageComplete(userInfo) {
   ISS.DOM.text('#payment_form_email','E-Mail: ' + userInfo.maTV);
   ISS.DOM.setVal('#payment_form_first_name',userInfo.ten);
   ISS.DOM.setVal('#payment_form_last_name',userInfo.ho);
   ISS.DOM.setVal('#payment_form_phone',userInfo.soDT);
   ISS.DOM.setVal('#payment_form_address',userInfo.diaChi);
   ISS.DOM.setVal('#payment_form_gender',userInfo.gioiTinh.toUpperCase());
   if(!userInfo.ngaySinh){
      ISS.DOM.setVal('#payment_form_day', '');
      ISS.DOM.setVal('#payment_form_month', '');
      ISS.DOM.setVal('#payment_form_year', '');
   }
   else {
      var _ngaySinh = new Date(userInfo.ngaySinh);
      ISS.DOM.setVal('#payment_form_day', ISS.DATE.getDateString(_ngaySinh));
      ISS.DOM.setVal('#payment_form_month', ISS.DATE.getMonthString(_ngaySinh));
      ISS.DOM.setVal('#payment_form_year', _ngaySinh.getFullYear());
   }
}
